package com.example.cheeseerpadmin.sys.service.impl;

import com.example.cheeseerpadmin.sys.entity.SysRole;
import com.example.cheeseerpadmin.sys.mapper.SysRoleMapper;
import com.example.cheeseerpadmin.sys.service.SysRoleService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.cheeseerpadmin.sys.vo.RoleVo;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author cheese
 * @since 2022-02-07
 */
@Service
@Transactional
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Override
    public boolean removeById(Serializable id) {
        //根据角色id去sys_role_permission当中删除对应的关系
        this.getBaseMapper().deleteRolePermissionByRid(id);
        //根据角色id去sys_role_user当中删除对应的关系
        this.getBaseMapper().deleteRoleUserByRid(id);

        return super.removeById(id);
    }

    /**
     * 重现服务接口当中的方法，具体的实现过程
     *
     * @param roleId
     * @return
     */
    @Override
    public List<Integer> queryRolePermissionIdsByRid(Integer roleId) {
        return this.getBaseMapper().queryRolePermissionIdsByRid(roleId);
    }

    /**
     * 重写服务接口当中的保存权限方法，实现其具体流程
     *
     * @param roleVo
     */
    @Override
    public void saveRolePermission(RoleVo roleVo) {
        SysRoleMapper baseMapper = this.getBaseMapper();
        Integer id = roleVo.getId();
        baseMapper.deleteRolePermissionByRid(roleVo.getId());
        if (roleVo.getIds() != null && roleVo.getIds().length > 0) {
            for (Integer pid : roleVo.getIds()) {
                baseMapper.saveRolePermission(roleVo.getId(), pid);
            }
        }
    }


}
